'--------------------------------------------------------'
' Description:
'   This script intends for printing pin's net name to its pad
'--------------------------------------------------------'
Option Explicit

' Add any type libraries to be used. 
Scripting.AddTypeLibrary("MGCPCB.ExpeditionPCBApplication")

CALL RemovePartNetUL

Function RemovePartNetUL()
	CALL RemoveUserLayer("Values_bot")
	CALL RemoveUserLayer("Values_top")
End Function

Function RemoveUserLayer(userLayerName)
    Dim pcbDoc
    Set pcbDoc = RetainActiveDoc()

    ' Get User Layer "PartValue"
    ' If not Exist create it
    ' See if the user layer already exists.
    Dim usrLyrObj
    Set usrLyrObj = pcbDoc.FindUserLayer(userLayerName)
    If usrLyrObj Is Nothing Then
    Else
        ' It does exist, remove any gfx on the user layer
        pcbDoc.UserLayerTexts(, userLayerName).Delete
        usrLyrObj.Delete
    End If
End Function

Function RetainActiveDoc()
' Get the application object 
    Dim pcbApp
    Set pcbApp = Application

    ' Get the active document 
    Dim pcbDoc
    Set pcbDoc = pcbApp.ActiveDocument

    ' License the document 
    ValidateServer(pcbDoc)

    Set RetainActiveDoc = pcbDoc
End Function

' Server validation function
Function ValidateServer(docObj)
  
  Dim keyInt
  Dim licenseTokenInt
  Dim licenseServerObj

  ' Ask Expedition’s document for the key
  keyInt = docObj.Validate(0)

  ' Get license server
  Set licenseServerObj = CreateObject("MGCPCBAutomationLicensing.Application")

  ' Ask the license server for the license token
  licenseTokenInt = licenseServerObj.GetToken(keyInt)

  ' Release license server
  Set licenseServerObj = nothing

  ' Turn off error messages (validate may fail if the token is incorrect)
  On Error Resume Next
  Err.Clear

  ' Ask the document to validate the license token
  docObj.Validate(licenseTokenInt)
  If Err Then
	  ValidateServer = 0    
  Else
	  ValidateServer = 1
  End If

End Function
